//
//  OrderStatusCell.m
//  QoilPublic
//
//  Created by SunChao on 17/9/11.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "OrderStatusCell.h"
#import "QColorUtility.h"
#import "SheetView.h"
#import "PPDragDropBadgeView.h"
#import "SheetView1.h"

//#import "NSString+SBJSON.h"
@interface OrderStatusCell()
//@property (weak, nonatomic) IBOutlet UILabel *OrderTitleLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftLeading;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightLeading;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btnWidth;

@property (weak, nonatomic) IBOutlet UILabel *firstLabel;
@property (weak, nonatomic) IBOutlet UILabel *secondLabel;
@property (weak, nonatomic) IBOutlet UILabel *thirdLabel;
@property (weak, nonatomic) IBOutlet UILabel *fourLabel;
@property (weak, nonatomic) IBOutlet UILabel *fiveLabel;
@property (weak, nonatomic) IBOutlet UILabel *sixLabel;

@property (weak, nonatomic) IBOutlet UIButton *firstBtn;
@property (weak, nonatomic) IBOutlet UIButton *secondBtn;
@property (weak, nonatomic) IBOutlet UIButton *thirdBtn;
@property (weak, nonatomic) IBOutlet UIButton *fourBtn;
@property (weak, nonatomic) IBOutlet UIButton *fiveBtn;
@property (weak, nonatomic) IBOutlet UIButton *sixBtn;


@property (strong, nonatomic)  PPDragDropBadgeView *imgView1;
@property (strong, nonatomic)  PPDragDropBadgeView *imgView2;
@property (strong, nonatomic)  PPDragDropBadgeView *imgView3;
@property (strong, nonatomic)  PPDragDropBadgeView *imgView4;
@property (strong, nonatomic)  PPDragDropBadgeView *imgView5;
@property (strong, nonatomic)  PPDragDropBadgeView *imgView6;

@property (nonatomic,strong)NSMutableArray *btnArr;
@property (nonatomic,strong)NSMutableArray *labelArr;
@property (nonatomic,strong)NSMutableArray *imgArr;

@property (nonatomic,strong)SheetView *sheetView;

@property (nonatomic,strong)NSMutableArray *evalArr;


@end
@implementation OrderStatusCell

- (void)awakeFromNib {
    [super awakeFromNib];
//    UIImageView *imgView = [[UIImageView alloc]initWithFrame:self.frame];
//    imgView.image = [UIImage imageNamed:@"myMission_cell_bg111"];
//    imgView.alpha = 0.5;
//    self.backgroundView = imgView;
   //self.textLabel.text = @"100";
    //self.backgroundColor  = [UIColor redColor];
//    _firstBtn.layer.cornerRadius = 25;
//    _firstBtn.layer.masksToBounds = YES;
//    _secondBtn.layer.cornerRadius = 25;
//    _secondBtn.layer.masksToBounds = YES;
//    _thirdBtn.layer.cornerRadius = 25;
//    _thirdBtn.layer.masksToBounds = YES;
//    _fourBtn.layer.cornerRadius = 25;
//    _fourBtn.layer.masksToBounds = YES;
//    _fiveBtn.layer.cornerRadius = 25;
//    _fiveBtn.layer.masksToBounds = YES;
//    _sixBtn.layer.cornerRadius = 25;
//    _sixBtn.layer.masksToBounds = YES;
    [_firstBtn setTitle:@"下单" forState:UIControlStateNormal];
    [_firstBtn setTintColor:[UIColor redColor]];
    
    
    if (iphone6Plus_5_5||kDevice_Is_iPhoneX) {
        _leftLeading = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.orderNum attribute:NSLayoutAttributeLeft multiplier:1.0 constant:-50.0];
        
        _btnWidth = [NSLayoutConstraint constraintWithItem:self.firstBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:70.0];
        //把约束添加到父视图上
        NSArray *array = [NSArray arrayWithObjects:_leftLeading,_btnWidth, nil];
        [self addConstraints:array];
    }
    if (iphone6_4_7) {
        _leftLeading = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.orderNum attribute:NSLayoutAttributeLeft multiplier:1.0 constant:-40.0];
        
        _btnWidth = [NSLayoutConstraint constraintWithItem:self.firstBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:60.0];
        //把约束添加到父视图上
        NSArray *array = [NSArray arrayWithObjects:_leftLeading,_btnWidth, nil];
        [self addConstraints:array];
    }
    if (iphone5x_4_0) {
        _leftLeading = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.orderNum attribute:NSLayoutAttributeLeft multiplier:1.0 constant:-20.0];
        
        _btnWidth = [NSLayoutConstraint constraintWithItem:self.firstBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:30.0];
        //把约束添加到父视图上
        NSArray *array = [NSArray arrayWithObjects:_leftLeading,_btnWidth, nil];
        [self addConstraints:array];
    }
   
//    float W = 0;
//    float x = 0;
//    float y = 0;
//    W = _firstBtn.bounds.size.width/4;
//    x = _firstBtn.bounds.origin.x+_firstBtn.bounds.size.width-W;
//    y = _firstBtn.bounds.origin.y+W;
//    //    }
//    
//    
//    _imgView1 = [[PPDragDropBadgeView alloc] initWithFrame:CGRectMake(x, y, W, W) dragdropCompletion:^{
//        NSLog(@"Drag Drop Done.");
//    }];
//    
//    [_firstBtn addSubview:_imgView1];
//    
//    
//    _imgView2 = [[PPDragDropBadgeView alloc] initWithFrame:CGRectMake(x, y, W, W) dragdropCompletion:^{
//        NSLog(@"Drag Drop Done.");
//    }];
//    
//    [_secondBtn addSubview:_imgView2];
//    
//    
//    _imgView3 = [[PPDragDropBadgeView alloc] initWithFrame:CGRectMake(x, y, W, W) dragdropCompletion:^{
//        NSLog(@"Drag Drop Done.");
//    }];
//    
//    [_thirdBtn addSubview:_imgView3];
//    
//    
//    _imgView4 = [[PPDragDropBadgeView alloc] initWithFrame:CGRectMake(x, y, W, W) dragdropCompletion:^{
//        NSLog(@"Drag Drop Done.");
//    }];
//    
//    [_fourBtn addSubview:_imgView4];
//    
//    _imgView5 = [[PPDragDropBadgeView alloc] initWithFrame:CGRectMake(x, y, W, W) dragdropCompletion:^{
//        NSLog(@"Drag Drop Done.");
//    }];
//    
//    [_fiveBtn addSubview:_imgView5];
//    
//    _imgView6 = [[PPDragDropBadgeView alloc] initWithFrame:CGRectMake(x, y, W, W) dragdropCompletion:^{
//        NSLog(@"Drag Drop Done.");
//    }];
//    
//    [_sixBtn addSubview:_imgView6];

}

- (void)setFrame:(CGRect)frame{
    frame.origin.x += 10;
    frame.origin.y += 10;
    frame.size.height -= 10;
    frame.size.width -= 20;
    [super setFrame:frame];
}

- (void)setupCellWithData:(NSArray *)timeArr statusText:(NSArray *)statusText purId:(NSString *)purId{
    
    NSMutableArray *imgNorArr = [NSMutableArray arrayWithObjects:@"下单-灰",@"配送-灰",@"加油-灰",@"完成-灰",@"付款-灰",@"评价-灰", nil];
    
    NSMutableArray *imgSelArr = [NSMutableArray arrayWithObjects:@"下单-蓝",@"配送-蓝",@"加油-蓝",@"完成-蓝",@"付款-蓝",@"评价-蓝",nil];

    NSMutableArray *array = [NSMutableArray new];
    for (NSString *tmpStr  in statusText) {
        
        NSString *str = [NSString stringWithFormat:@"%@-蓝",tmpStr];
        
        [array addObject:str];
        
    }
    
    for (int i = 0; i<array.count; i++) {
        [imgSelArr replaceObjectAtIndex:i withObject:[array objectAtIndex:i]];
    }
    
    _btnArr = [NSMutableArray array];
    
    [_btnArr addObject:_firstBtn];
    [_btnArr addObject:_secondBtn];
    [_btnArr addObject:_thirdBtn];
    [_btnArr addObject:_fourBtn];
    [_btnArr addObject:_fiveBtn];
    [_btnArr addObject:_sixBtn];
    
    
    _labelArr = [NSMutableArray array];
    [_labelArr addObject:_firstLabel];
    [_labelArr addObject:_secondLabel];
    [_labelArr addObject:_thirdLabel];
    [_labelArr addObject:_fourLabel];
    [_labelArr addObject:_fiveLabel];
    [_labelArr addObject:_sixLabel];
    
//    _imgArr = [NSMutableArray array];
//    
//    
//    [_imgArr addObject:_imgView1];
//    [_imgArr addObject:_imgView2];
//    [_imgArr addObject:_imgView3];
//    [_imgArr addObject:_imgView4];
//    [_imgArr addObject:_imgView5];
//    [_imgArr addObject:_imgView6];
    
    
    for (int i = 0; i<timeArr.count; i++) {
        
       UILabel *label =  [_labelArr objectAtIndex:i];
        label.text = [timeArr objectAtIndex:i];
    }
    
    
    for (int i = 0; i<_btnArr.count; i++) {
        UIButton *button = [_btnArr objectAtIndex:i];

        NSString *norImg = [imgNorArr objectAtIndex:i];
        NSString *selImg = [imgSelArr objectAtIndex:i];
        [button setImage:[UIImage imageNamed:norImg] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:selImg] forState:UIControlStateSelected];
        button.layer.cornerRadius = 25;
        button.layer.masksToBounds = YES;
    }
    _status = (int)statusText.count;
   // NSString *lastText = [statusText lastObject];
    for (int i = 0; i<_btnArr.count; i++) {
        
        if (i<_status) {
            
            UIButton *button = [_btnArr objectAtIndex:i];
            button.selected = YES;
            UILabel *label = [_labelArr objectAtIndex:i];
            label.hidden = NO;

        }else{
            
            UIButton *button = [_btnArr objectAtIndex:i];
            button.selected = NO;
            UILabel *label = [_labelArr objectAtIndex:i];
            label.hidden = YES;
            
        }
    }
//    for (int i = 0; i<_imgArr.count; i++) {
//        
//        UIImageView *imgView = [_imgArr objectAtIndex:i];
//        
//        imgView.hidden = YES;
//    }
//    
//    UIImageView *imgView = [_imgArr objectAtIndex:_status-1];
//    
//    imgView.hidden = NO;
    
}
- (IBAction)btnAction:(UIButton *)button{
    
    NSLog(@"buttonTag:%ld",(long)button.tag);
    
   
    if (button.tag == 15&&button.selected == YES) {
        
        [self requestData];
    }
    
}

-(void)drawRect:(CGRect)rect{
    // 1.获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    CGContextMoveToPoint(ctx, _firstBtn.center.x, _firstBtn.center.y);
   int index = 0;
    for (id object in _btnArr) {
        if ([object isKindOfClass:[UIButton class]]) {
           UIButton *btn = (UIButton *)object;
             index = (int)btn.tag;
             UIColor *color = [QColorUtility colorWithHexString:@"3090e2"];
           [color set];
             CGContextAddLineToPoint(ctx, btn.center.x, btn.center.y);
        }
    }
    
   CGContextSetLineWidth(ctx, 2.0f);
   
   CGContextStrokePath(ctx);     UIGraphicsPushContext(ctx);
   [self drawNewRect];

}
- (void)drawNewRect{
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    int index = 0;
    for (int i = 0; i<_btnArr.count; i++) {
        UIButton *btn = [_btnArr objectAtIndex:i];
        if (btn.selected == YES) {
            index =(int)btn.tag-10;
        }
    }
    UIButton *btn = (UIButton *)[_btnArr objectAtIndex:index];
    
    CGContextMoveToPoint(ctx, btn.center.x, btn.center.y); // 起点3090e2
    for (int i = index; i<_btnArr.count; i++) {
        UIColor *color = [QColorUtility colorWithHexString:@"b5b5b5"];
        [color set];
        UIButton *btn = [_btnArr objectAtIndex:i];
        CGContextAddLineToPoint(ctx, btn.center.x, btn.center.y); //终点
    }
    
    CGContextSetLineWidth(ctx, 2.0f); // 线的宽度
    
    CGContextStrokePath(ctx); // 渲染（直线只能绘制空心的，不能调用CGContextFillPath(ctx);）
    
}


/*- (void)loadClicked
{
    _sheetView = [[SheetView alloc]init];
    
    [_sheetView showInView:[self getCurrentVC].view];
    
    
}*/

- (void)showPopView
{
    
    SheetView1 *sheet = [[SheetView1 alloc]init];
      sheet.dataSource = _evalArr;
    UIViewController *vc = [self getCurrentVC];
    [sheet showInView:vc.view];
    
}

- (void)requestData{
    
    NSString *url = [QParamService urlWithAction:@"selUserEvaluatetanker"];
    NSDictionary *para= [QParamService GainValuationDataWithOrderId:_orderId];
    
    [[QHttpManager sharedManager] POST:url parameterMD5:para completion:^(NSData *data, NSDictionary *dict, NSError *error) {
       
        if(error){
            if([QUserInfo sharedInfo].isHaveNetWork == NO){
               
            }
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            
            NSDictionary *dic =  basModel.items;
            if ([dic.allKeys containsObject:@"evalJsonstr"]) {
               NSString *evalJsonstr = [dic objectForKey:@"evalJsonstr"];
               
               NSString *string = [evalJsonstr stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\""]];
                if (string == nil) {
                    
                }
                
                NSData *jsonData = [string dataUsingEncoding:NSUTF8StringEncoding];
                NSError *err;
                _evalArr = [NSMutableArray new];
                _evalArr = [NSJSONSerialization JSONObjectWithData:jsonData
                                                                    options:NSJSONReadingMutableContainers
                                                                      error:&err];
                
                if (_evalArr.count>0) {
                    
                    NSString *content = [dic objectForKey:@"content"];
                    if (content != nil ||![content isEqualToString:@""]) {
                        NSDictionary *dic = @{@"content":content};
                         [_evalArr addObject:dic];
                    }
                    [self showPopView];
                    
                }
                if(err) {
                    NSLog(@"json解析失败：%@",err);  
                    
                }
            }
        }
    }];

}


//获取当前屏幕显示的viewcontroller
- (UIViewController *)getCurrentVC
{
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    
    return result;
}
@end
